﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using Microsoft.Win32;

namespace YArchitech.HCLoad
{
	public static class HCLoadUtility
	{
		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public static Process GetHCloadProcess()
		{
			Process[] processesByName = Process.GetProcessesByName("HCLoad");
			if (processesByName == null || processesByName.Length == 0)
			{
				return null;
			}
			Process process = processesByName[0];
			if (process.HasExited)
			{
				process.Start();
			}
			return process;
		}

		public static Process StartHCLoad(string fileName)
		{
			Process result;
			try
			{
				Process process = new Process();
				process.StartInfo.FileName = HCLoadUtility.HCLoadExeFile;
				if (File.Exists(fileName))
				{
					process.StartInfo.Arguments = "\"" + fileName + "\"";
				}
				process.Start();
				result = process;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public static void SendMessageToReadGbxml(Process process, string fileName)
		{
			if (process == null)
			{
				return;
			}
			string text = Path.GetDirectoryName(Path.GetDirectoryName(process.MainModule.FileName));
			if (HCLoadUtility.m_sHCLoadVersion.Equals("8.0"))
			{
				text = Environment.ExpandEnvironmentVariables("%appdata%\\YArchitech\\HCLoad8.0\\User\\");
			}
			else if (HCLoadUtility.m_sHCLoadVersion.Equals("9.0"))
			{
				text = Environment.ExpandEnvironmentVariables("%appdata%\\YArchitech\\HCLoad9.0\\User\\");
			}
			text = Path.Combine(text, "HCLoad.ini");
			IniHelper.WriteSetting("GBXML", "FileName", fileName, text);
			HCLoadUtility.PostMessage(process.MainWindowHandle, 49151, IntPtr.Zero, IntPtr.Zero);
		}

		public static bool IsInatallHCLoad70(out string errorMessage)
		{
			errorMessage = "";
			string text = HCLoadUtility.GetInstallHCLoadLocation();
			if (string.IsNullOrEmpty(text))
			{
				text = HCLoadUtility.GetRegistryHCLoadLocation();
				if (string.IsNullOrEmpty(text))
				{
					text = HCLoadUtility.GetAcsHCLoadLocation();
					if (string.IsNullOrEmpty(text) && !HCLoadUtility.ManualSelectLocation(ref text))
					{
						errorMessage = "没有安装盈建科负荷计算软件，不能运行!";
						return false;
					}
				}
			}
			if (text.ToUpper().Contains(HCLoadUtility.InstallVersion9.ToUpper()))
			{
				HCLoadUtility.HCLoadExeFile = Path.Combine(text, HCLoadUtility.HCLoadEXE9);
			}
			else if (text.ToUpper().Contains(HCLoadUtility.InstallVersion.ToUpper()))
			{
				HCLoadUtility.HCLoadExeFile = Path.Combine(text, HCLoadUtility.HCLoadEXE);
			}
			if (!File.Exists(HCLoadUtility.HCLoadExeFile))
			{
				errorMessage = HCLoadUtility.HCLoadExeFile + "不存在!" + Environment.NewLine + "没有找到盈建科负荷计算软件启动程序，不能运行此命令。";
				return false;
			}
			return true;
		}

		private static bool ManualSelectLocation(ref string hcloadlocation)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.Description = "请选择盈建科负荷计算软件的安装路径";
			if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			hcloadlocation = folderBrowserDialog.SelectedPath;
			string path = "";
			if (hcloadlocation.ToUpper().Contains(HCLoadUtility.InstallVersion9.ToUpper()))
			{
				hcloadlocation = hcloadlocation.Substring(0, hcloadlocation.ToUpper().IndexOf(HCLoadUtility.InstallVersion9.ToUpper(), 0));
				hcloadlocation = Path.Combine(hcloadlocation, HCLoadUtility.InstallVersion9);
				path = Path.Combine(hcloadlocation, HCLoadUtility.HCLoadEXE9);
			}
			else if (hcloadlocation.ToUpper().Contains(HCLoadUtility.InstallVersion.ToUpper()))
			{
				hcloadlocation = hcloadlocation.Substring(0, hcloadlocation.ToUpper().IndexOf(HCLoadUtility.InstallVersion.ToUpper(), 0));
				hcloadlocation = Path.Combine(hcloadlocation, HCLoadUtility.InstallVersion);
				path = Path.Combine(hcloadlocation, HCLoadUtility.HCLoadEXE);
			}
			if (!File.Exists(path))
			{
				MessageBox.Show("没有找到盈建科负荷计算启动程序, 请重新选择");
				HCLoadUtility.ManualSelectLocation(ref hcloadlocation);
			}
			return true;
		}

		private static bool IsNullOrEmpty(string[] strings)
		{
			return strings == null || strings.Length == 0;
		}

		private static string GetInstallHCLoadLocation()
		{
			string installLocation = Product.InstallLocation;
			string text = "";
			int num = installLocation.TrimEnd(new char[]
			{
				'\\'
			}).LastIndexOf("\\");
			if (num > 0)
			{
				string path = installLocation.Substring(0, num);
				string setting = IniHelper.GetSetting("HCLoad", "InstallVersion", Product.ConfigFile, "HCLoad8.0");
				if (setting == "HCLoad8.0")
				{
					if (File.Exists(Path.Combine(path, setting, HCLoadUtility.HCLoadEXE)))
					{
						text = Path.Combine(path, setting);
					}
					if (HCLoadUtility.HCLoadPathValid(text, 8))
					{
						return text;
					}
				}
				else if (setting == "HCLoad9.0")
				{
					if (File.Exists(Path.Combine(path, setting, HCLoadUtility.HCLoadEXE9)))
					{
						text = Path.Combine(path, setting);
					}
					if (HCLoadUtility.HCLoadPathValid(text, 9))
					{
						return text;
					}
				}
			}
			return string.Empty;
		}

		private static string GetRegistryHCLoadLocation()
		{
			for (int i = 9; i >= 8; i--)
			{
				string text = string.Empty;
				text = HCLoadUtility.GetRegistryHCLoad80Location(i);
				string path = "";
				if (i == 9)
				{
					path = Path.Combine(text, HCLoadUtility.HCLoadEXE9);
				}
				else if (i == 8)
				{
					path = Path.Combine(text, HCLoadUtility.HCLoadEXE);
				}
				if (File.Exists(path))
				{
					HCLoadUtility.m_sHCLoadVersion = i.ToString() + ".0";
					return text;
				}
			}
			return string.Empty;
		}

		private static string GetRegistryHCLoad80Location(int nVer)
		{
			string str = "";
			if (nVer == 8)
			{
				str = "8.0\\HcLoad-235";
			}
			else if (nVer == 9)
			{
				str = "9.0\\HcLoad-235";
			}
			string name = "SOFTWARE\\Wow6432Node\\Hongye\\HcLoad\\" + str;
			string name2 = "SOFTWARE\\Hongye\\HcLoad\\" + str;
			string text = string.Empty;
			using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(name))
			{
				if (registryKey != null)
				{
					text = (string)registryKey.GetValue("Location", string.Empty);
				}
				if (string.IsNullOrEmpty(text))
				{
					RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey(name2);
					if (registryKey2 != null)
					{
						text = (string)registryKey2.GetValue("Location", string.Empty);
					}
				}
			}
			if (HCLoadUtility.HCLoadPathValid(text, nVer))
			{
				return text;
			}
			return string.Empty;
		}

		private static void SaveRegistryHCLoadLocation(string location, string strVersion)
		{
			if (string.IsNullOrEmpty(location))
			{
				return;
			}
			string text = "7.0\\HcLoad-214";
			string text2 = "8.0\\HcLoad-235";
			string text3 = "SOFTWARE\\Wow6432Node\\Hongye\\HcLoad\\";
			text3 += (strVersion.Equals("8") ? text2 : text);
			RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(text3, true);
			if (registryKey == null)
			{
				registryKey = Registry.LocalMachine.CreateSubKey(text3);
			}
			if (registryKey != null)
			{
				registryKey.SetValue("Location", location);
				registryKey.Close();
			}
		}

		private static int GetHCLoadCompiledDate()
		{
			if (!string.IsNullOrEmpty(HCLoadUtility.HCLoadRelease))
			{
				HCLoadUtility.HCLoadRelease = HCLoadUtility.HCLoadRelease.Substring(0, HCLoadUtility.HCLoadRelease.LastIndexOf("."));
				HCLoadUtility.HCLoadRelease = HCLoadUtility.HCLoadRelease.Substring(HCLoadUtility.HCLoadRelease.LastIndexOf(".") + 1);
				return int.Parse(HCLoadUtility.HCLoadRelease);
			}
			return 0;
		}

		private static int VersionComparison(string v1, string v2)
		{
			double num;
			double num2;
			if (!double.TryParse(v1, out num) || !double.TryParse(v2, out num2))
			{
				return 0;
			}
			if (num > num2)
			{
				return 1;
			}
			if (num < num2)
			{
				return -1;
			}
			return 0;
		}

		private static bool GetAcsLocationBySoftId(out string acsLocation, RegistryKey hyAcsVerKey, string softwareId)
		{
			acsLocation = string.Empty;
			RegistryKey registryKey = hyAcsVerKey.OpenSubKey(softwareId);
			if (registryKey == null)
			{
				return false;
			}
			object value = registryKey.GetValue("Location");
			if (value == null)
			{
				return false;
			}
			string text = Convert.ToString(value);
			if (Directory.Exists(text))
			{
				acsLocation = text;
				return true;
			}
			return false;
		}

		private static bool GetAcsLocationByVersion(out string acsLocation, RegistryKey hyAcsKey, string versionString)
		{
			acsLocation = string.Empty;
			RegistryKey registryKey = hyAcsKey.OpenSubKey(versionString);
			if (registryKey == null)
			{
				return false;
			}
			string[] subKeyNames = registryKey.GetSubKeyNames();
			if (subKeyNames == null || subKeyNames.Length == 0)
			{
				return false;
			}
			foreach (string softwareId in subKeyNames)
			{
				string text;
				if (HCLoadUtility.GetAcsLocationBySoftId(out text, registryKey, softwareId))
				{
					acsLocation = text;
					return true;
				}
			}
			return false;
		}

		private static bool GetAcsLocation(out string acsLocation)
		{
			acsLocation = string.Empty;
			RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Hongye\\ACS");
			if (registryKey == null)
			{
				registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Hongye\\ACS");
			}
			if (registryKey == null)
			{
				return false;
			}
			string[] subKeyNames = registryKey.GetSubKeyNames();
			if (subKeyNames == null || subKeyNames.Length == 0)
			{
				return false;
			}
			List<string> list = new List<string>(subKeyNames);
			list.Sort(new Comparison<string>(HCLoadUtility.VersionComparison));
			foreach (string versionString in list)
			{
				string text;
				if (HCLoadUtility.GetAcsLocationByVersion(out text, registryKey, versionString))
				{
					acsLocation = text;
					return true;
				}
			}
			return false;
		}

		private static string GetAcsHCLoadLocation()
		{
			string empty = string.Empty;
			if (!HCLoadUtility.GetAcsLocation(out empty))
			{
				return string.Empty;
			}
			DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(empty, "HCLOAD"));
			if (!directoryInfo.Exists)
			{
				return string.Empty;
			}
			DirectoryInfo[] directories = directoryInfo.GetDirectories();
			if (directories == null || directories.Length == 0)
			{
				return string.Empty;
			}
			DirectoryInfo[] array = directories;
			for (int i = 0; i < array.Length; i++)
			{
				string fullName = array[i].FullName;
				if (HCLoadUtility.HCLoadPathValid(fullName, 9))
				{
					return fullName;
				}
				if (HCLoadUtility.HCLoadPathValid(fullName, 8))
				{
					return fullName;
				}
			}
			return string.Empty;
		}

		private static bool HCLoadPathValid(string hcloadPath, int nVer)
		{
			string path = "";
			if (nVer == 8)
			{
				path = Path.Combine(hcloadPath, HCLoadUtility.HCLoadEXE);
			}
			else if (nVer == 9)
			{
				path = Path.Combine(hcloadPath, HCLoadUtility.HCLoadEXE9);
			}
			return File.Exists(path);
		}

		private static string InstallVersion = "HCLoad8.0";

		private static string HcLoadRelativePath = "Sys\\R17";

		private static string HCLoadEXE = HCLoadUtility.HcLoadRelativePath + "\\HCLoad.exe";

		private static string InstallVersion9 = "HCLoad9.0";

		private static string HcLoadRelativePath9 = "Sys\\R19";

		private static string HCLoadEXE9 = HCLoadUtility.HcLoadRelativePath9 + "\\HCLoad.exe";

		private static string HCLoadRelease = string.Empty;

		public static string HCLoadExeFile;

		private static string m_sHCLoadVersion = "7.0";

		private const int WM_GBXML = 49151;
	}
}
